﻿#region Summary

// 
// Owner : JackieHan 
// 
// Solution : Erp 
// 
// Project : Jackie.Utility.BvConversion
// 
// File : DefaultValueConversionFacotry.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 默认的值转换工厂
// 
// History : 
//			2012-05-15 11:48:55	    Create 
// 

#endregion

#region Using

using System;
using System.Collections;
using System.Collections.Generic;
using Jackie.Utility.BvConversion.DefalutValueConversion;

#endregion

namespace Jackie.Utility.BvConversion
{
    ///<summary>
    ///默认的值转换工厂
    ///</summary>
    public class DefaultValueConversionFacotry : IValueConversionFactory
    {
        #region Implementation of IValueConversionFactory

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public virtual IValueConversion CreateValueConversion(Type bType, Type vType)
        {
            if (string.IsNullOrEmpty(bType.FullName)) throw new ArgumentException("btype");
            if (string.IsNullOrEmpty(vType.FullName)) throw new ArgumentException("vtype");

            if (bType.FullName.Equals(vType.FullName))
                return new DefaultValueConversion();

            if (bType.FullName.Equals(typeof(DateTime).FullName) && vType.FullName.Equals(typeof(string).FullName))
                return new DateTimeStringValueConversion();
            if (bType.IsEnum && vType.FullName.Equals(typeof(string).FullName))
                return new EnumStringValueConversion(bType);
            if (bType.FullName.Equals(typeof(decimal).FullName) && vType.FullName.Equals(typeof(string).FullName))
                return new DecimalStringValueConversion();
            if (bType.FullName.Equals(typeof(int).FullName) && vType.FullName.Equals(typeof(string).FullName))
                return new IntStringValueConversion();

            if ((typeof(IEnumerable).IsAssignableFrom(bType)) && vType.FullName.Equals(typeof(string).FullName))
                return new CollectionStringValueConversion();

            if (bType.IsClass && vType.FullName.Equals(typeof(string).FullName))
                return new ClassStringValueConversion();

            throw new Exception(string.Format("btype:{0} vtype:{1} is not IValueConversion implement!", bType.FullName,
                                              vType.FullName));
        }

        #endregion
    }
}